<?php


namespace app\service\user;


use app\model\config\CouponModel;
use app\model\user\UserCouponModel;
use app\service\BaseService;

class CouponService extends BaseService
{

    public function __construct()
    {
        $this->model = new CouponModel();
    }

    /**
     * 获取用户可领优惠券列表
     * @param $userId
     * @return array
     * @throws \Exception
     */
    public function getCouponList($userId)
    {
        $list = $this->getListByWhere([['use_end_time', '>=', time()], ['type', '=', 0]], 'id,coupon_name,amount,use_end_time,use_type');
        $list1 = [];
        $i = 0;
        $userCouponService = new UserCouponService();
        foreach ($list as $key => $value) {
            $isGet = $userCouponService->getOneLineByWhere(['user_id' => $userId, 'coupon_id' => $value['id']]);
            if (!$isGet) {
                $list1[$i] = $value;
                $i++;
            }
        }
        return $list1;
    }
}